<template>
  <div class="reply">
    <!--左边用户信息-->
    <div class="user-info">
      <!--头像-->
      <el-avatar :src="avatarUrl" @click.native="openUserPage" shape="square" :size="72"></el-avatar>
      <!--昵称-->
      <div @click="openUserPage">{{ reply.nickname }}</div>
    </div>
    <!--右边回复类容-->
    <div class="content">
      <div>
        {{ reply.content }}
      </div>
      <div class="content-foot">
        <span>{{ floor }}楼</span>
        <span style="margin: 0 10px;">{{ reply.formatDate }}</span>
        <span @click="dialogFormVisible = true">回复</span>
      </div>
      <!--楼中楼-->
      <div class="content-footer" v-if="reply.replyInnerList.length>0">
        <div v-for="(item) in reply.replyInnerList" :key="item.id">
          <!--<el-divider v-if="index !== 0"></el-divider>-->
          <div class="content-footer-item">
            <el-avatar :src="replyInnerAvatar(item.avatar)" style="margin-right: 10px;"
                       @click="openUserPage(item.userId)"></el-avatar>
            <div class="content-footer-item-right">
              <div>
                <!--昵称-->
                <span @click="openUserPage(item.userId)">{{ item.nickname }}: </span>
                <!--内容-->
                <span v-html="item.content"></span>
              </div>
              <!--时间-->
              <div>
                {{ item.formatDate }}
              </div>
            </div>
          </div>
          <!--头像-->
        </div>
      </div>
    </div>

    <!--回复表单-->
    <el-dialog :visible.sync="dialogFormVisible" width="615px" :show-close="false" center>
      <!--上传信息-->
      <el-form label-position="left" :model="replyInner">
        <!--内容-->
        <el-form-item>
          <!--emoji-->
          <div style="font-size: 20px">
            <span v-for="emoji in emojiList" :key="emoji" @click="replyInner.content+=emoji" style="cursor: default">
              {{ emoji }}
            </span>
          </div>
          <el-input type="textarea" :rows="4" v-model="replyInner.content" ref="content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addReplyInner">回 复</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { baseUrl } from '@/utils/const'
import { getId } from '@/utils/auth'
import { replyReply } from '@/api/reply'

export default {
  name: 'Reply',
  data () {
    return {
      replyInner: {
        content: ''
      },
      dialogFormVisible: false,
      emojiList: ['☺️', '😆', '😅', '😭', '😋', '🤮', '🤗', '😱', '🤣', '😄', '😍', '🤡', '👿', '🙉', '👨🏻‍🦳', '❤️', '🙏🏻']
    }
  },
  props: {
    reply: {
      required: true
    },
    index: {
      required: true
    }
  },
  computed: {
    avatarUrl () {
      return baseUrl + this.reply.avatar
    },
    floor () {
      return this.index + 2
    }
  },
  methods: {
    openUserPage (userId) {
      if (userId) {
        const routeData = this.$router.resolve({
          path: '/userPage',
          query: {
            id: userId
          }
        })
        window.open(routeData.href)
      } else {
        const routeData = this.$router.resolve({
          path: '/userPage',
          query: {
            id: this.reply.userId
          }
        })
        window.open(routeData.href)
      }
    },
    // 添加楼中楼
    async addReplyInner () {
      this.replyInner.date = new Date()
      this.replyInner.userId = getId()
      this.replyInner.replyId = this.reply.id
      this.dialogFormVisible = false
      await replyReply(this.replyInner)
    },
    // 楼中楼头像
    replyInnerAvatar (url) {
      return baseUrl + url
    },
    // emoji
    emojiUrl (url) {
      return baseUrl + url
    }
  }
}
</script>

<style scoped lang="less">
.reply {
  display: flex;

  .user-info {
    width: 200px;
    display: flex;
    flex-direction: column;
    align-items: center;

    .el-avatar {
      cursor: pointer;
    }

    div {
      width: 150px;
      color: @menuColor;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      text-align: center;
      cursor: pointer;
    }
  }

  .content {
    width: 650px;
    display: flex;
    flex-direction: column;

    .content-foot {
      margin-top: 20px;
      font-size: small;
      display: flex;
      justify-content: flex-end;

      span:nth-child(3) {
        cursor: pointer;
      }

      span:nth-child(3):hover {
        color: @menuColor;
      }
    }

    .content-footer {
      //border: 1px solid @borderBase;
      margin-top: 10px;
      padding: 10px;

      .el-divider--horizontal {
        margin: 10px 0;
      }

      .content-footer-item {
        display: flex;
        margin: 20px 0;

        .content-footer-item-right {
          width: 100%;

          div:first-child {
            align-items: center;
          }

          span:first-child {
            color: @menuColor;
            cursor: pointer;
          }

          div:nth-child(2) {
            font-size: small;
            display: flex;
            justify-content: flex-end;
          }
        }
      }
    }
  }
}
</style>
